using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Windows.Data;

namespace Nizm0.QOT.Library.UserControls
{
    public partial class ucShowServerInfo : UserControl
    {
        System.Threading.Thread _databaseFeelThread;
        private Objects.Server _server;

        public Objects.Server Server
        {
            get { return _server; }
            set { _server = value; }
        }

        public delegate void EventChange(object sender);
        /// <summary>
        /// when tmpserver address whill be chaned
        /// </summary>
        /// <param name="sender">string with new address</param>
        public event EventChange ServerAdressChange;

        public ucShowServerInfo(Library.Objects.Server server)
            : this()
        {
            _server = server;
            //InitializeComponent();
            Fill(_server);

            textBox_Name.Enabled = false;
            textBox_SeverAdress.Enabled = false;

            //checkedListBox1.Items.AddRange(Enum.GetNames(typeof(Objects.Server.TypeOfServer)));
            //TypeOfServerFlagConverter t = new TypeOfServerFlagConverter();

            //for (int i = 0; i < checkedListBox1.Items.Count; i++)// var o in checkedListBox1.Items)
            //{
            //    checkedListBox1.SetItemChecked(i, (bool)t.Convert(_server.Type, typeof(Objects.Server.TypeOfServer), checkedListBox1.Items[i], System.Globalization.CultureInfo.CurrentCulture));
            //    //t.Convert(server.Type, typeof(Objects.Server.TypeOfServer), o, System.Globalization.CultureInfo.CurrentCulture);
            //}
        }

        public ucShowServerInfo()
        {
            InitializeComponent();
            comboBox_LoginType.SelectedIndex = 0;
            checkedListBox1.Items.AddRange(Enum.GetNames(typeof(Objects.Server.TypeOfServer)));
            checkedListBox1.Items.RemoveAt(0);
        }

        public void Fill(Library.Objects.Server server)
        {
            _server = server;
            textBox_Name.Text = server.Address;
            textBox_SeverAdress.Text = server.Address;
            if (server.SSPI)
            {
                //comboBox_LoginType.Text = comboBox_LoginType.Items[0].ToString();
                comboBox_LoginType.SelectedIndex = 0;
                panel_Login.Enabled = false;
            }
            else
            {
                //comboBox_LoginType.Text = comboBox_LoginType.Items[1].ToString();
                comboBox_LoginType.SelectedIndex = 1;
                panel_Login.Enabled = true;
            }
            if (panel_Login.Enabled)
            {
                textBox_Login.Text = server.Login;
                textBox_Password.Text = server.Password;
            }

            if (_databaseFeelThread != null)
            {
                _databaseFeelThread.Abort();
                //System.Threading.wa
            }

            _databaseFeelThread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(FeelDataBases));
            //FeelDataBases(server);
            _databaseFeelThread.Start(server);

            if (comboBox_Database.Items.Count == 0)
            {
                comboBox_Database.Text = "No Databases";
            }
            if (server.ActualDataBase != null)
            {
                comboBox_Database.SelectedIndex = comboBox_Database.Items.IndexOf(server.ActualDataBase.DataBaseName);
                //comboBox_Database.Text = tmpserver.ActualDataBase.DataBaseName;
            }
            //comboBox_ServerType.Text = tmpserver.Type.ToString();
            //comboBox_ServerType.SelectedIndex = (int)server.Type;
            TypeOfServerFlagConverter t = new TypeOfServerFlagConverter();

            for (int i = 0; i < checkedListBox1.Items.Count; i++)// var o in checkedListBox1.Items)
            {
                //if (checkedListBox1.Items[i].ToString() == Objects.Server.TypeOfServer.NoUsed.ToString())
                //{
                //    checkedListBox1.SetItemChecked(i, (bool)t.Convert(server.Type, typeof(Objects.Server.TypeOfServer), checkedListBox1.Items[i], System.Globalization.CultureInfo.CurrentCulture));
                //    break;
                //}
                //else
                checkedListBox1.SetItemChecked(i, (bool)t.Convert(server.Type, typeof(Objects.Server.TypeOfServer), checkedListBox1.Items[i], System.Globalization.CultureInfo.CurrentCulture));
                //t.Convert(server.Type, typeof(Objects.Server.TypeOfServer), o, System.Globalization.CultureInfo.CurrentCulture);
            }

            checkBox_UseInThisProject.Checked = server.UseInThisProject;
        }

        public void Clear()
        {
            _server = null;
            textBox_Name.Text = null;
            textBox_Name.Enabled = true;
            textBox_SeverAdress.Text = null;
            textBox_SeverAdress.Enabled = true;
                //comboBox_LoginType.Text = comboBox_LoginType.Items[0].ToString();
                comboBox_LoginType.SelectedIndex = 0;
                panel_Login.Enabled = false;

            if (_databaseFeelThread != null)
            {
                _databaseFeelThread.Abort();
                //System.Threading.wa
            }

            //_databaseFeelThread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(FeelDataBases));
            ////FeelDataBases(server);
            //_databaseFeelThread.Start(server);

            comboBox_Database.Text = "No Databases";
            
            //comboBox_Database.SelectedIndex = comboBox_Database.Items.IndexOf(server.ActualDataBase.DataBaseName);

            TypeOfServerFlagConverter t = new TypeOfServerFlagConverter();

            for (int i = 0; i < checkedListBox1.Items.Count; i++)// var o in checkedListBox1.Items)
            {
                checkedListBox1.SetItemChecked(i, (bool)t.Convert(Objects.Server.TypeOfServer.NoUsed, typeof(Objects.Server.TypeOfServer), checkedListBox1.Items[i], System.Globalization.CultureInfo.CurrentCulture));
                //t.Convert(server.Type, typeof(Objects.Server.TypeOfServer), o, System.Globalization.CultureInfo.CurrentCulture);
            }

            checkBox_UseInThisProject.Checked = false;
        }

        public void FeelDataBases(Object obj)
        {
            Library.Objects.Server server = (Library.Objects.Server)obj;
            //comboBox_Database.Items.Clear();

            if (server.DataBase == null || server.DataBase.Count == 0)
            {
                foreach (string tmpDataBase in Library.Scripts.GetFromSQL.GetDataBases(server))
                {
                    if (this.InvokeRequired)
                        this.BeginInvoke((MethodInvoker)delegate
                        {
                            comboBox_Database.Items.Add(tmpDataBase);
                        });
                    else
                        comboBox_Database.Items.Add(tmpDataBase);
                }
            }
            else
            {
                System.Collections.ArrayList tmpDatabaseList = Library.Scripts.GetFromSQL.GetDataBases(server);
                if (server.DataBase.Count != tmpDatabaseList.Count)
                {
                    foreach (string tmpDatabase in tmpDatabaseList)
                    {
                        lock (server.DataBase)
                        {
                            if (!server.DataBase.Exists(d => d.Name == tmpDatabase))
                            {
                                //if (this.InvokeRequired)
                                //    this.BeginInvoke((MethodInvoker)delegate
                                //    {
                                lock (server.DataBase)
                                    server.DataBase.Add(new Library.Objects.DataBase(tmpDatabase));
                                //    });
                                //else
                                //    server.DataBase.Add(tmpDatabase, new Library.Objects.DataBase(tmpDatabase));
                            }
                        }
                    }
                }
            }
        }

        public Library.Objects.Server ReturnServer()
        {
            if (_server == null)
                _server = new Library.Objects.Server(textBox_SeverAdress.Text.Trim());

            _server.Name = textBox_Name.Text;
            _server.Address = textBox_SeverAdress.Text;

            if (comboBox_LoginType.SelectedIndex == 0)
            {
                _server.SSPI = true;
            }
            else
            {
                _server.SSPI = false;
                _server.Login = textBox_Login.Text.Trim();
                _server.Password = textBox_Password.Text.Trim();
            }
            _server.ActualDataBase = new Library.Objects.DataBase(comboBox_Database.Text);
            _server.UseInThisProject = checkBox_UseInThisProject.Checked;

            TypeOfServerFlagConverter t = new TypeOfServerFlagConverter();
            if (checkedListBox1.CheckedItems.Count > 0)
            {
                _server.Type = Objects.Server.TypeOfServer.NoUsed;
                foreach (object o in checkedListBox1.CheckedItems)
                {
                    _server.Type |= (Objects.Server.TypeOfServer)Enum.Parse(typeof(Objects.Server.TypeOfServer), o.ToString());
                    //var a = t.Convert(tmpserver.Type, typeof(Objects.Server.TypeOfServer), o, System.Globalization.CultureInfo.CurrentCulture);
                }
            }
            else
                _server.Type = Objects.Server.TypeOfServer.NoUsed;
            return _server;
        }

        private void comboBox_LoginType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox_LoginType.SelectedIndex == 0)
            {
                //comboBox_LoginType.Text = comboBox_LoginType.Items[0].ToString();
                //comboBox_LoginType.SelectedIndex = 0;
                panel_Login.Enabled = false;
            }
            else
            {
                //comboBox_LoginType.Text = comboBox_LoginType.Items[1].ToString();
                //comboBox_LoginType.SelectedIndex = 1;
                panel_Login.Enabled = true;
            }
        }

        private void textBox_SeverAdress_TextChanged(object sender, EventArgs e)
        {
            textBox_Name.Text = textBox_SeverAdress.Text;
            if (ServerAdressChange != null)
                ServerAdressChange(textBox_SeverAdress.Text);
        }
    }

    public class TypeOfServerFlagConverter : IValueConverter
    {

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            //bool result;
            if (value.GetType() != typeof(Objects.Server.TypeOfServer))

                throw new InvalidOperationException("value must be of type Objects.Server.TypeOfServer");

            Objects.Server.TypeOfServer type = (Objects.Server.TypeOfServer)Enum.Parse(typeof(Objects.Server.TypeOfServer), parameter.ToString());

            if (Objects.Server.TypeOfServer.NoUsed == type && Objects.Server.TypeOfServer.NoUsed == (Objects.Server.TypeOfServer)value)
                return true;
            return Objects.Server.TypeOfServer.NoUsed == type ? false : (((Objects.Server.TypeOfServer)value) & type) == type;

            //return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {

            if (value.GetType() != typeof(Objects.Server.TypeOfServer))

                throw new InvalidOperationException("value must be of type Objects.Server.TypeOfServer");

            Objects.Server.TypeOfServer typeofserver = (Objects.Server.TypeOfServer)Enum.Parse(typeof(Objects.Server.TypeOfServer), parameter.ToString());

            throw new Exception("// return value - Objects.Server.TypeOfServer");

        }

        #endregion

    }

}
